package com.ip.getip.service.impl;

import com.alibaba.fastjson.JSON;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import jdk.nashorn.internal.runtime.regexp.joni.Regex;
import org.apache.commons.codec.binary.Base64;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * @ Description   :  动态解析腾讯云域名
 * @ Author        :  leixin
 * @ CreateDate    :  2019/10/23-23:54
 * @ Version       :  1.0
 */
@Component
public class DDnsImpl {
    @Scheduled(cron="${ddns_cron}")
    //获取当前外网ip地址
    public void getIp() throws Exception {
        String inputLine = "";
        String read = "";
        URL url = new URL("http://icanhazip.com/");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        while ((read = in.readLine()) != null) {
            inputLine += read;
        }
        String ddns = updateDDns(inputLine,"844600769","www");
        System.out.println(JSON.parse(ddns));
        String ddns1 = updateDDns(inputLine,"844600770", "@");
        System.out.println(JSON.parse(ddns1));
    }

    //修改解析记录
    public static String updateDDns(String ip,String recordId,String subDomain) throws Exception {
        String url ="https://dnsapi.cn/Record.Modify";
        RestTemplate resetTemplate = new RestTemplate();
        MultiValueMap<String, Object> requestMap= new LinkedMultiValueMap<String, Object>();
        requestMap.add("login_token", "292765,81fd91cd5837fd0706344e2cab0be16e");
        requestMap.add("format","json");
        requestMap.add("lang","cn");
        requestMap.add("domain", "leixin.xyz");
        requestMap.add("record_id",recordId);
        requestMap.add("sub_domain",subDomain);
        requestMap.add("record_type","A");
        requestMap.add("record_line","默认");
        requestMap.add("value",ip);
//        requestMap.add("mx {1-20}",1);
        HttpEntity requestEntity = new HttpEntity(requestMap,null);
        ResponseEntity<String> forEntity = resetTemplate.postForEntity(url,requestEntity,String.class);
        return forEntity.getBody();
    }

    public static void main(String[] args) throws Exception {
        String s = dDnsList();
        System.out.println(JSON.parse(s));
    }

    //获取解析记录列表
    public static String dDnsList() throws Exception {
        String url ="https://dnsapi.cn/Record.List";
        RestTemplate resetTemplate = new RestTemplate();
        MultiValueMap<String, Object> requestMap= new LinkedMultiValueMap<String, Object>();
        requestMap.add("login_token", "292765,81fd91cd5837fd0706344e2cab0be16e");
        requestMap.add("format","json");
        requestMap.add("lang","cn");
        requestMap.add("domain", "leixin.xyz");
        HttpEntity requestEntity = new HttpEntity(requestMap,null);
        ResponseEntity<String> forEntity = resetTemplate.postForEntity(url,requestEntity,String.class);
        return forEntity.getBody();
    }
}
